package com.tensquare.base.service;

import cn.hutool.core.util.StrUtil;
import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import com.tensquare.entity.IdWorker;
import com.tensquare.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zzb
 * @create 2020/12/14
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;
    //添加标签
    public void add(Label label) {
        //设置主键
        long id = idWorker.nextId();
        label.setId(id+"");
        //保存
        labelDao.save(label);
    }

    //查询所有标签
    public List<Label> findAll() {
        return labelDao.findAll();
    }

    //查询单个标签
    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }

    //修改标签
    public void update(Label label) {
        labelDao.save(label);
    }

    //根据id删除标签
    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }

    //进行条件查询
    public List<Label> search(Label label) {
        //进行条件查询
        Specification<Label> specification = createSpecification(label);

        //返回查询结果
        return labelDao.findAll(specification);
    }

    //进行条件查询
    private Specification<Label> createSpecification(Label label) {
        //HQL:hibernate Query Language
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //定义查询条件的集合
                List<Predicate> predicateList = new ArrayList<>();
                //判断传入的参数是否有值
                if (label != null){
                    if (StrUtil.isNotBlank(label.getLabelname())){  //标签名
                        predicateList.add(cb.like(root.get("labelname").as(String.class),"%"+label.getLabelname()+"%"));
                    }
                    if (StrUtil.isNotBlank(label.getState())){      //状态值
                        predicateList.add(cb.equal(root.get("state").as(String.class),label.getState()));
                    }
                    if (StrUtil.isNotBlank(label.getRecommend())){  //是否推荐
                        predicateList.add(cb.equal(root.get("recommend").as(String.class),label.getRecommend()));
                    }
                }
                //定义与条件集合大小一致的数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                //将集合中的内容放到数组中
                Predicate[] predicates1 = predicateList.toArray(predicates);
                //返回
                return cb.and(predicates1);
            }
        };
    }

    //分页带条件查询
    public PageResult<Label> search(Label label, Integer page, Integer size) {
        //进行分页查询
        Page<Label> labelPage = labelDao.findAll(createSpecification(label), PageRequest.of(page - 1, size));
        //返回分页结果
        return new PageResult<>(labelPage.getTotalElements(),labelPage.getContent());
    }
}
